// pages/toShaidan/toShaidan.js
const app = getApp()
var url = require('../../utils/url.js');
var token;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    files: [],
    filesImg: [],
    content:"",
    order:""
  },
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      count: 9, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function (res) {
        // success
        console.log(res)
        var uploudImg = res.tempFilePaths[0];


        wx.uploadFile({
          url: url.URl + 'file/upload',
          filePath: uploudImg,
          name: 'file',
          formData: { type: 1 },
          header: {
            "Content-Type": "multipart/form-data",
            'Authorization': token
          },
          success: function (res) {
            console.log(res)
            var data = JSON.parse(res.data)
            console.log(data)
            console.log(url)
            var newwImg = url.URl2 + data.data
            console.log(newwImg)
            if (data.code == 200) {
              wx.showToast({
                title: '图片上传成功',
              })
              that.setData({
                files: that.data.files.concat(newwImg),
                filesImg: that.data.files.concat(data.data),
              })
            } else {

            }

          },
          fail: function (res) {
            wx.hideToast();
            wx.showModal({
              title: '错误提示',
              content: '上传图片失败',
              showCancel: false,
              success: function (res) { }
            })
          }
        })
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  },
  previewImage: function (e) {
    wx.previewImage({
      current: e.currentTarget.id, // 当前显示图片的http链接
      urls: this.data.files // 需要预览的图片http链接列表
    })
  },
  content:function(e){
     this.setData({
       content:e.detail.value
     })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //  console.log(options.item)
     var JsonItem=options.item;
     var order = JSON.parse(JsonItem)
     this.setData({
       order:order
     })
     console.log(order)
     wx.getStorage({
       key: 'token',
       success: function(res) {
         token=res.data
       },
     })
  },
  submit:function(){
     var that=this;
     wx.showLoading({
       title: '加载中...',
     })
    var order=that.data.order;
    var content1=that.data.content;
    var filesImg = that.data.filesImg;
    if (content1.length==0){
      wx.showToast({
        title: '请填写',
        icon:"none"
      })
    } else if (filesImg.length==0){
      wx.showToast({
        title: '请晒出照片',
      })
    }else{
      var data={};
      data.images = filesImg.join(",");
      data.content = content1;
      data.id = order.id;
      wx.request({
        url: url.URl + 'my/treasure/shaidan',
        data: data,
        method: 'post',
        header: {
          'Authorization': token
        },
        success: function (res) {
          console.log(res)
          wx.hideLoading()
          var msg = res.data.msg
          if (res.data.code == 200) {
            wx.showToast({
              title: msg,
              icon: 'none',
              duration: 2000,
              success:function(){
                 wx.navigateBack({
                   delta: 1,
                 })
              }
            })
          } else {
            wx.showToast({
              title: msg,
              icon: 'none',
              duration: 2000
            })
          }
        }
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})